//
// libgraphics_sfml.cpp for nibbler in /home/lefebv_w//workspace/cpp/nibbler/v1
// 
// Made by damien lefebvre
// Login   <lefebv_w@epitech.net>
// 
// Started on  Tue Mar 12 16:42:52 2013 damien lefebvre
// Last update Fri Jul 26 17:35:55 2013 mickael gomes
//

#include		"IGraphicLibrary.hh"
#include		"LibGraphic_sfml.hh"

// Class PictureOfElement

LibGraphic_SFML::PictureOfElement::PictureOfElement(std::string const & path, IGraphicLibrary::e_ListOfImage type)
  : m_path(path), m_type(type)
{
  if (!m_image.LoadFromFile(path))
    {
      std::cerr << "Cannot load image : " << m_path << std::endl;
    }
}

bool		LibGraphic_SFML::PictureOfElement::isThatType(IGraphicLibrary::e_ListOfImage type) const
{
  return (m_type == type);
}

sf::Image const &	LibGraphic_SFML::PictureOfElement::getImage(void) const
{
  return (m_image);
}

IGraphicLibrary::e_ListOfImage	LibGraphic_SFML::PictureOfElement::getType(void) const
{
  return (m_type);
}

// Class LibGraphic_SFML

LibGraphic_SFML::LibGraphic_SFML(unsigned int const size_x, unsigned int const size_y, std::vector<IGraphicLibrary::InfoPicture> const & listOfElement)
  : m_graph(sf::VideoMode(size_x, size_y, 32), DEFAULT_WINDOWS_NAME), m_size_x(size_x), m_size_y(size_y), m_compteur(0)
{
  for (unsigned int i = 0; i < listOfElement.size(); i++)
    {
      m_pictures.push_back(PictureOfElement(listOfElement[i].getPath(), listOfElement[i].getType()));
    }
}

void			LibGraphic_SFML::refreshScreen(void)
{
  m_graph.Display();
}

unsigned int		LibGraphic_SFML::find_image(IGraphicLibrary::e_ListOfImage e)
{
  for (unsigned int i = 0; i < m_pictures.size(); i++)
    {
      if (m_pictures[i].getType() == e)
	return (i);
    }
  return (-1);
}
      
void			LibGraphic_SFML::draw(unsigned int x, unsigned int y, IGraphicLibrary::e_ListOfImage const e, IGraphicLibrary::e_Direction const dir)
{
  sf::Sprite					sp;
  unsigned int					idImg;
  sf::Vector2f					size;
  
  x = x * SIZE_CASE;
  y = y * SIZE_CASE;
  idImg = find_image(e);
  if (idImg == -1)
    return ;
  sp.SetImage(m_pictures[idImg].getImage());
  if (dir != IGraphicLibrary::UP)
    {
      size = sp.GetSize();
      sp.Rotate(90 * dir);
      switch (dir)
	{
	case IGraphicLibrary::RIGHT :
	  sp.SetPosition(x + size.x, y);
	  break;
	case IGraphicLibrary::LEFT :
	  sp.SetPosition(x, y + size.y + 1);
	  break;
	case IGraphicLibrary::DOWN :
	  sp.SetPosition(x + size.x + 1, y + size.y);
	  break;
	}
    }
  else
    sp.SetPosition(x, y);
  m_graph.Draw(sp);
}

IGraphicLibrary::e_TypeOfEvent		LibGraphic_SFML::checkEvent(void)
{
  sf::Event				event;

  while (true)
    {
      if (!m_graph.GetEvent(event))
  	return (IGraphicLibrary::NONE);
      
      if (event.Type == sf::Event::Closed)
  	return (IGraphicLibrary::EXIT);

      if (event.Type == sf::Event::KeyPressed)
  	{
  	  switch (event.Key.Code)
  	    {
  	    case sf::Key::Escape :
  	      return (IGraphicLibrary::EXIT);
	      
  	    case sf::Key::P :
  	      return (IGraphicLibrary::PAUSE);
	      
  	    case sf::Key::Up :
  	      return (IGraphicLibrary::KB_UP);
	      
  	    case sf::Key::Down :
  	      return (IGraphicLibrary::KB_DOWN);
	      
  	    case sf::Key::Left :
  	      return (IGraphicLibrary::KB_LEFT);
	      
  	    case sf::Key::Right :
  	      return (IGraphicLibrary::KB_RIGHT);
  	    }
  	}
    } 
}

void	LibGraphic_SFML::draw_footer(unsigned int x, unsigned int y, std::string const & str)
{
  sf::String	text;
  sf::Shape	rect;

  x = x * SIZE_CASE;
  y = y * SIZE_CASE;
  rect = sf::Shape::Rectangle(x, y, m_size_x, m_size_y, sf::Color(32, 32, 32), 2, sf::Color::Black);
  text.SetText(str);
  text.SetFont(sf::Font::GetDefaultFont());
  text.SetSize(30);
  text.SetPosition(x + 16, y);
  text.SetColor(sf::Color::White);
  m_graph.Draw(rect);
  m_graph.Draw(text);
}

extern "C"
{
  IGraphicLibrary	*make_IGraphicLibrary(unsigned int size_x, unsigned int size_y, std::vector<IGraphicLibrary::InfoPicture> const & list)
  {
    return (new LibGraphic_SFML(size_x, size_y, list));
  }
}
